from django.contrib import admin
from django.shortcuts import render,HttpResponse,redirect
from crm import models
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField
# Register your models here.

class CustomerAdmin(admin.ModelAdmin):
    list_display = ('id','name','qq','source','consultant','content','status','date') #显示
    list_filter = ('source','consultant','date')  #查询
    search_fields = ('qq','name')  #搜索
    raw_id_fields = ('consult_course',)
    filter_horizontal = ('tags',)
    list_editable = ('status',)

    # actions = ["test_action", ]
    #
    #
    # def test_action(self,request,arg2):
    #     print('test action:',self,request,arg2)
        # return render(request,"king_admin/table_index.html")
class UserProfileAdmin(admin.ModelAdmin):
    list_display = ('user','name')

class CourseRecordAdmin(admin.ModelAdmin):
    list_display = ['from_class','day_num','teacher','has_homework','homework_title','date']

    def initialize_studyrecords(self, request, queryset):
        print('--->initialize_studyrecords', self, request, queryset)
        if len(queryset) > 1:
            return HttpResponse("只能选择一个活动")
        print(queryset[0].from_class.enrollment_set.all())
        new_obj_list = []
        for enroll_obj in queryset[0].from_class.enrollment_set.all():
            models.StudyRecord.objects.get_or_create(
                student = enroll_obj,
                course_record = queryset[0] ,
                attendance = 0 ,
                score = 0 ,
            )
            # new_obj_list.append(models.StudyRecord(
            #     student=enroll_obj,
            #     course_record=queryset[0],
            #     attendance=0,
            #     score=0,
            # ))
            # try:
            #     models.StudyRecord.objects.bulk_create(new_obj_list)  # 批量创建
            # except Exception as e:
            #     return HttpResponse("批量初始化学习记录失败，请检查该节课是否已经有对应的学习记录")

        return redirect("/admin/crm/studyrecord/?course_record__id__exact=%s"%queryset[0].id)

    initialize_studyrecords.short_description = "初始化本次活动所有学生的活动记录"
    actions = ['initialize_studyrecords', ]

class StudyRecordAdmin(admin.ModelAdmin):
    list_display = ['student','course_record','attendance','score','date']  #显示
    list_filter = ['course_record','score','attendance','course_record__from_class','student']  #过滤
    list_editable = ['score','attendance']  #可在表单编辑

admin.site.register(models.Customer,CustomerAdmin)
admin.site.register(models.CustomerFollowUp)
admin.site.register(models.Enrollment)
admin.site.register(models.Course)
admin.site.register(models.ClassList)
admin.site.register(models.CourseRecord,CourseRecordAdmin)
admin.site.register(models.Branch)
admin.site.register(models.Role)
admin.site.register(models.Payment)
admin.site.register(models.StudyRecord,StudyRecordAdmin)
admin.site.register(models.Tag)
admin.site.register(models.UserProfile,UserProfileAdmin)
admin.site.register(models.Menu)